var ctx = $("#ctx").val();
$(function () {
    var qun = getParam("qun");
    if (qun != null) {
        dataList(qun);
    }

    //搜索框展开的时候回车搜索
    $(document).keyup(function (event) {
        if (event.keyCode == 13) {
            $("#search-group").trigger("click");
        }
    });
    $("body").on("click", ".search-div .mdui-textfield-expanded #search-group", function () {
        dataList($("#qun").val());
    });
});

function dataList(qq) {
    $(".mdui-progress").css("display", "block");
    $(".dataList").html("");
    $.ajax({
        url: ctx + "/group/group",
        type: "GET",
        data: {"qun": qq},
        success: function (data) {
            if (data.code == 0) {
                var list = data.data;
                if (list.length > 0) {
                    $.each(list, function (index, value) {
                        var tbody = "<tr>" +
                            "<td>" + formatData(value.id) + "</td>" +
                            "<td><a target='_blank' href='"+ctx+"/search/qq?qq="+formatData(value.qqNum)+"'>" + formatData(value.qqNum) + "</a></td>" +
                            "<td>" + formatData(value.nick) + "</td>" +
                            "<td>" + formatData(value.age) + "</td>" +
                            "<td>" + formatSex(value.gender) + "</td>" +
                            "<td>" + formatAuth(value.auth) + "</td>" +
                            "<td>" + formatData(value.qunNum) + "</td>" +
                            "</tr>";
                        $(".dataList").append(tbody);
                    })
                } else {
                    $(".dataList").append("<tr><td colspan='7' style='text-align: center'>无数据</td></tr>");
                }
                mdui.mutation();
            } else {
                mdui.snackbar({
                    message: data.msg,
                    position: "top"
                });
            }
            $(".mdui-progress").css("display", "none");
        }
    })
}

/*获取URL中参数*/
var getParam = function (name) {
    var search = document.location.search;
    var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
    var matcher = pattern.exec(search);
    var items = null;
    if (null != matcher) {
        try {
            items = decodeURIComponent(decodeURIComponent(matcher[1]));
        } catch (e) {
            try {
                items = decodeURIComponent(matcher[1]);
            } catch (e) {
                items = matcher[1];
            }
        }
    }
    return items;
};

function formatData(data) {
    if (data == null) {
        return "";
    } else {
        return data;
    }
}

function formatSex(data) {
    if (data == null) {
        return "";
    }
    if (data == 1) {
        return "女"
    }
    if (data == 0) {
        return "男"
    }
}

function formatAuth(data) {
    if (data == null) {
        return "";
    }
    if (data == 4) {
        return "群主"
    }
    if (data == 2) {
        return "管理员"
    }
    if (data == 1) {
        return "群成员"
    }
}